﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Memo.Electricity.Engine;
using Memo.Electricity.Setting;

namespace Memo.Electricity.Game
{
	public class CBelt : MemoBehaviour
	{
		public float Speed
        {
			get => mSpeed;
        }
		[SerializeField] private int mNodeCount;
		[SerializeField] private float mSpeed;

#if UNITY_EDITOR
		public void GenerateBelt()
		{
			if (mNodeCount < 2)
			{
				Debug.LogError("传送带节点数至少为2");
				return;
			}

			//加载配置
			var setting = Resources.Load<BeltSetting>("GameSetting/BeltSetting");
			//清理
			bool flag = true;
			while (transform.childCount > 0 && flag)
			{
				DestroyImmediate(transform.GetChild(0).gameObject);
			}
			//准备生成
			mBeltNodes = new List<GameObject>();
			GameObject go;
			//生成左端点
			go = GameObject.Instantiate(setting.Left, transform);
			go.name = go.name.Replace("(clone)", "");
			go.transform.localPosition = Vector3.zero;
			mBeltNodes.Add(go.GetComponent<GameObject>());
			//生成中间节点
			for(int i=1;i< mNodeCount - 1;i++)
			{
				go = GameObject.Instantiate(setting.Middle, transform);
				go.name = go.name.Replace("(clone)", "");
				go.transform.localPosition = Vector3.zero + i * Vector3.right;
				mBeltNodes.Add(go.GetComponent<GameObject>());
			}
			//生成右端点
			go = GameObject.Instantiate(setting.Right, transform);
			go.name = go.name.Replace("(clone)", "");
			go.transform.localPosition = Vector3.zero + (mNodeCount - 1) * Vector3.right;
			mBeltNodes.Add(go.GetComponent<GameObject>());
			//生成碰撞体(子物体)
			GameObject colliderObject = new GameObject("BeltCollider");
			colliderObject.transform.parent = transform;
			colliderObject.transform.localPosition = Vector3.zero;
			BoxCollider2D collider = colliderObject.AddComponent<BoxCollider2D>();
			collider.size = new Vector3(mNodeCount, 0.5f, 0);
			collider.offset = Vector3.right * (collider.size.x - 1) * 0.5f;
			//生成触发器
			EdgeCollider2D trigger = null;
			if (!gameObject.TryGetComponent(out trigger))
				trigger = gameObject.AddComponent<EdgeCollider2D>();
			trigger.isTrigger = true;
			Vector2[] vertices = new Vector2[2];
			vertices[0] = new Vector2(-0.5f, 0.3f);
			vertices[1] = new Vector2(mNodeCount - 0.5f, 0.3f);
			trigger.points = vertices;
		}

#endif

		private List<GameObject> mBeltNodes;

		private List<Transform> mTransObjects; // 被传送的物体

		private void Awake()
		{
			mTransObjects = new List<Transform>();
		}

		private void OnTriggerEnter2D(Collider2D collision)
		{
			if (collision.GetComponent<Rigidbody2D>() != null)
				mTransObjects.Add(collision.transform);
		}

		private void OnTriggerExit2D(Collider2D collision)
		{
			if (mTransObjects.Contains(collision.transform))
			{
				mTransObjects.Remove(collision.transform);
				if (collision.CompareTag("Player"))
				{
					CharcterControlTest cct = collision.GetComponent<CharcterControlTest>();
					cct.onBelt = false;
					cct.beltSpeed = 0;
				}
			}
				
		}

		private void Update()
		{
			foreach (var tran in mTransObjects)
			{
				if (tran.CompareTag("Player"))
				{
					CharcterControlTest cct = tran.GetComponent<CharcterControlTest>();
					cct.onBelt = true;
					cct.beltSpeed = mSpeed;
				}
				else
				{
					tran.Translate(Vector3.right * Speed * Time.deltaTime);
				}
				
			}
		}

	}
}
